# PYTHONDEVMODE to enable PYTHONASYNCIODEBUG=1 and other flags.
# PYTHONTRACEMALLOC=1 is helpful when debugging ResourceWarnings, but is too
# slow to leave enabled all the time.
PYTHON_ENVIRONMENT := PYTHONASYNCIODEBUG=1 PYTHONDEBUG=1 PYTHONWARNINGS=always::ResourceWarning

ifeq ($(VERBOSE),1)
    VERBOSE_OPTION_DJANGO_TESTS:= -v 2
    VERBOSE_OPTION_TESTS := -v
else
    VERBOSE_OPTION_DJANGO_TESTS :=
    VERBOSE_OPTION_TESTS :=
endif

# Quick sanity check to run before each commit
check:
	pre-commit run -a

# Reformat the code and show changes made by black
format black:
	tox -e format

djlint:
	tox -e djlint

coverage:
	python3 -m coverage erase
	$(PYTHON_ENVIRONMENT) python3 -m coverage run ./manage.py test debusine.db debusine.django debusine.server debusine.web debusine.project debusine.test $(VERBOSE_OPTION_DJANGO_TESTS)
	$(PYTHON_ENVIRONMENT) DJANGO_SETTINGS_MODULE=debusine.signing.settings python3 -m coverage run ./manage.py test debusine.signing $(VERBOSE_OPTION_DJANGO_TESTS)
	$(PYTHON_ENVIRONMENT) python3 -m coverage run -m unittest discover debusine.artifacts $(VERBOSE_OPTION_TESTS)
	$(PYTHON_ENVIRONMENT) python3 -m coverage run -m unittest discover debusine.assets $(VERBOSE_OPTION_TESTS)
	$(PYTHON_ENVIRONMENT) python3 -m coverage run -m unittest discover debusine.client $(VERBOSE_OPTION_TESTS)
	$(PYTHON_ENVIRONMENT) python3 -m coverage run -m unittest discover debusine.tasks $(VERBOSE_OPTION_TESTS)
	$(PYTHON_ENVIRONMENT) python3 -m coverage run -m unittest discover debusine.worker $(VERBOSE_OPTION_TESTS)
	$(PYTHON_ENVIRONMENT) python3 -m coverage run -m unittest discover debusine.utils $(VERBOSE_OPTION_TESTS)
	python3 -m coverage combine
	python3 -m coverage html
	python3 -m coverage report --precision=2 --show-missing --skip-covered $(if $(TOTAL_COVERAGE),| perl -pe '$$total = $$1 if /^TOTAL .* (\d+(?:\.\d+)?%)/; END { print "TOTAL COVERAGE $$total\n" }')

css:
	( \
		echo "/* This file is generated by pygments by running make css. License is BSD-2-clause (same as pygments) */" ; \
		python3 -m pygments -S github-dark -f html \
	) > debusine/web/static/web/css/debusine-code-highlight.css

.PHONY: check format black djlint coverage css
